"""
    Author:
    Date: 2018-12-26

    微信管理后台-视频审核
"""

from django.shortcuts import render

from entities.consts import *
from entities import user_mgr
from entities import video_mgr
from entities import vchat_api_mgr
from utils import ajax
from utils.log import Log

from .. import helpers
from .. helpers import gma_login_required


@gma_login_required
def index(request):
    """ 代理管理首页 """
    context = helpers.build_context(request)
    context['waiting_auth_video_count'] = video_mgr.get_video_count_by_auth_status(VIDEO_AUTH_STATUS_WAITING, request)
    context['unauthorized_video_count'] = video_mgr.get_video_count_by_auth_status(VIDEO_AUTH_STATUS_REJECT, request)
    context['authorized_video_count'] = video_mgr.get_video_count_by_auth_status(VIDEO_AUTH_STATUS_AGREE, request)
    context['star5_video_count'] = video_mgr.get_video_count_by_star(5, request)
    context['star4_video_count'] = video_mgr.get_video_count_by_star(4, request)
    context['star3_video_count'] = video_mgr.get_video_count_by_star(3, request)
    context['star2_video_count'] = video_mgr.get_video_count_by_star(2, request)
    context['star1_video_count'] = video_mgr.get_video_count_by_star(1, request)
    context['star0_video_count'] = video_mgr.get_video_count_by_star(0, request)
    return render(request, 'gma/video/video_home.html', context)


@gma_login_required
def waiting_videos(request):
    """ 待审的视频列表 """
    context = helpers.build_context(request)
    context['videos'] = video_mgr.get_video_list_by_auth_status(VIDEO_AUTH_STATUS_WAITING, 100, request)
    return render(request, 'gma/video/video_list.html', context)


@gma_login_required
def rejected_videos(request):
    """ 审核被拒的视频列表 """
    context = helpers.build_context(request)
    context['videos'] = video_mgr.get_video_list_by_auth_status(VIDEO_AUTH_STATUS_REJECT, 100, request)
    return render(request, 'gma/video/video_list.html', context)


@gma_login_required
def agreed_videos(request):
    """ 审核通过的视频列表 """
    context = helpers.build_context(request)
    context['videos'] = video_mgr.get_video_list_by_auth_status(VIDEO_AUTH_STATUS_AGREE, 100, request)
    return render(request, 'gma/video/video_list.html', context)


@gma_login_required
def videos_by_star(request, star):
    """ 星级分类的视频列表 """
    videos = video_mgr.get_video_list_by_star(star, 100, request)
    context = helpers.build_context(request)
    context['videos'] = videos
    return render(request, 'gma/video/video_list.html', context)


@gma_login_required
def videos_sort_by_price(request):
    """ 价格倒序的视频列表 """
    videos = video_mgr.get_video_list_sort_by_price_desc(200, request)
    context = helpers.build_context(request)
    context['videos'] = videos
    return render(request, 'gma/video/video_list.html', context)


@gma_login_required
def auth_video(request, vid):
    """ 视频的审核页面 """
    video = video_mgr.get_video_info(vid)
    if video is None:
        return helpers.show_page_tips(request, '未找到指定的视频！')

    context = helpers.build_context(request)
    context['user'] = user_mgr.get_user_detail(video['uid'])
    context['video'] = video
    return render(request, 'gma/video/auth_video.html', context)


@gma_login_required
def api_agree_video(request):
    """ 同意短视频 """
    vid = request.POST['video_id'].strip()
    video = video_mgr.get_video_info(vid)
    if not video:
        return ajax.error_response('无效的视频id!')

    success = video_mgr.update_video_auth_status(vid, VIDEO_AUTH_STATUS_AGREE)
    if not success:
        Log.error(f'Agree video status failed: vid={vid}')
    else:
        Log.info(f'Agree video status ok: vid={vid}')

    data = {}
    return ajax.success_response(data)


@gma_login_required
def api_reject_video(request):
    """ 拒绝短视频 """
    vid = request.POST['video_id'].strip()
    video = video_mgr.get_video_info(vid)
    if not video:
        return ajax.error_response('无效的视频id!')

    success = video_mgr.update_video_auth_status(vid, VIDEO_AUTH_STATUS_REJECT)
    if not success:
        Log.error(f'Reject video status failed: vid={vid}')
    else:
        Log.info(f'Reject video status ok: vid={vid}')

    data = {}
    return ajax.success_response(data)


@gma_login_required
def api_set_video_star(request):
    """ 设置短视频星级 """
    vid = request.POST['video_id'].strip()
    star = int(request.POST['star'].strip())

    # 检验参数合法性
    if star < 0 or star > 5:
        return ajax.error_response('无效的星级!')

    video = video_mgr.get_video_info(vid)
    if not video:
        return ajax.error_response('无效的视频id!')

    success = video_mgr.update_video_star(vid, star)
    if not success:
        Log.error(f'Set video star failed: vid={vid}')
    else:
        Log.info(f'Set video star ok: vid={vid}')

    data = {}
    return ajax.success_response(data)


@gma_login_required
def api_set_video_price(request):
    """ 设置短视频价格 """
    vid = request.POST['vid'].strip()
    price = int(request.POST['price'].strip())

    # 检验参数合法性
    if price < 0 or price > 30:
        return ajax.error_response('无效的价格!')

    video = video_mgr.get_video_info(vid)
    if not video:
        return ajax.error_response('无效的视频id!')

    result = vchat_api_mgr.set_video_price(vid, price)
    Log.info(f'Request api result: {result}')
    return ajax.success_response()


@gma_login_required
def api_set_calling_video(request):
    """ 设置呼叫视频 """
    vid = request.POST['video_id'].strip()
    is_calling_video = int(request.POST['is_calling_video'].strip())

    # 检验参数合法性
    if is_calling_video not in [0, 1]:
        return ajax.error_response('无效的参数!')

    video_mgr.update_calling_video(vid, is_calling_video)
    return ajax.success_response()


if __name__ == '__main__':
    print('ok')
